<?php
declare (strict_types = 1);

namespace app\backend\controller;

use app\backend\model\Article;
use app\backend\model\ArticleCategory;

class ArticleReview extends Base
{
    // 权限控制中间件
    protected array $middleware = [
        'app\backend\middleware\Permission' => [
            'only' => ['index', 'look', 'review']
        ]
    ];

    // 待审核文章列表
    public function index()
    {
        return view();
    }

    // 我的文章列表数据接口
    public function list()
    {
        $page = request()->param('page');
        $limit = request()->param('limit');
        $title = request()->param('title');

        $articleList = Article::with(['articleCategory'])
            ->field('id,article_category_id,title,status,create_time,update_time')
            ->where([
                ['title', 'like', '%'.$title.'%'],
                ['status', '=', 1]
            ])
            ->paginate([
                'list_rows' => $limit,
                'var_page' => 'page',
                'page' => $page,
                'query' => [
                    'title' => $title,
                ]
            ]);
        $articleListData = $articleList->toArray()['data'];
        // 设置顶级分类
        foreach ($articleListData as &$v) {
            if ($v['article_category_id'] == 0) {
                $v['category'] = '顶级分类';
            }
        }
        unset($v);

        $data = [
            'code' => 0,
            'msg' => '',
            'count' => $articleList->total(),
            'data' => $articleListData
        ];

        return json($data);
    }

    // 查看文章
    public function look()
    {
        $id = (int)input('get.id');

        $article = Article::find($id);
        $article->articleContent;
        $article->articleCategory;
        $articleItem = $article->toArray();
        if (empty($articleItem['articleCategory'])) {
            $articleItem['articleCategory'] = ['id' => '_0'];
        }

        $categoryList = ArticleCategory::field('id,pid,title,level')->select()->toArray();
        $categoryList = get_cate_list($categoryList);

        // 给分类列表加上空格以区分父子级关系
        $cate = [];
        $cate['_0'] = '顶级分类';
        foreach ($categoryList as $category) {
            $space = '';
            for ($i = 0; $i < (int)$category['level']; $i++) {
                $space .= '&emsp;';
            }
            $cate['_'.$category['id']] = $space . $category['title'];
        }

        return view('', [
            'category' => $cate,
            'article' => $articleItem
        ]);
    }

    // 审核文章
    public function review()
    {
        $id = (int)input('post.id');
        $status = (int)input('post.status');

        if (!in_array($status, [0, 1])) {
            return json([
                'code' => 1,
                'msg' => '参数错误'
            ]);
        }

        if ($status == 1) {
            // 通过审核
            Article::update(['status' => 3], ['id' => $id]);
            $msg = '审核通过';
        } else {
            // 未通过审核
            Article::update(['status' => 2], ['id' => $id]);
            $msg = '审核未通过';
        }

        return json([
            'code' => 0,
            'msg' => $msg
        ]);
    }
}
